﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ITJZ.BitHelper.Local.UI.Component
{
    public partial class ucShowForJavascript : UserControl,IComponent.IShowKnowledge
    {
        public ucShowForJavascript()
        {
            InitializeComponent();
        }

        private void ucShow_Load(object sender, EventArgs e)
        {

            webBrowser1.DocumentText = "第一步：从上面文本框中选中要测试的代码<br/>第二步：点“复制选中部分到代码”<br/>第三步：点击“运行”，运行结果将显示在这里。<hr/>提示：代码框中的代码是可以修改的";
        }

        #region IShowKnowledge 成员


        public void Show(ITJZ.BitHelper.APIModel.Knowledge knowledge)
        {
            richTextBox1.Rtf = "";//清空格式


            label1.Text = knowledge.FullName;
            try
            {
                richTextBox1.Rtf = knowledge.Content;
            }
            catch { richTextBox1.Text = knowledge.Content; }

            //richTextBox1.Clear();
            //try
            //{
            //    using (MemoryStream ms = new MemoryStream())
            //    {
            //        StreamWriter write = new StreamWriter(ms);
            //        write.Write(knowledge.Content);
            //        ms.Position = 0;
            //        richTextBox1.LoadFile(ms, RichTextBoxStreamType.RichText);
            //    }
            //}
            //catch { richTextBox1.Text = knowledge.Content; }
        }

        #endregion


        private void llbRun_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {


            string html = string.Format(@"
try
{{
    {0}
}}
catch(ex)
{{
	document.write(""出现异常<br/>____________________________________<br/>"")

	document.write(ex);
}}
", richTextBox2.Text);



            webBrowser1.DocumentText = "<script>" + html + "</script>";
            webBrowser1.CreateControl();
        }

        private void llbToCode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
          richTextBox2.Text=  richTextBox1.SelectedText;
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
        }
    }
}
